import peewee
import peewee_async

import datetime

import json

from playhouse.shortcuts import model_to_dict

database = peewee_async.PooledMySQLDatabase(
    'myflask', host='127.0.0.1', port=3306,
    user='root', password='root')


from playhouse.shortcuts import model_to_dict
import json

def print_model(model):
	return json.dumps(model_to_dict(model), indent=4, sort_keys=True, default=str,ensure_ascii=False)

def print_models(models):
	return json.dumps(list(models.dicts()), indent=4, sort_keys=True, default=str,ensure_ascii=False)

# Define model
class TestNameModel(peewee.Model):

    name = peewee.CharField(null=False,default=None)
    age = peewee.IntegerField(null=False,default=0)
    reg_date = peewee.DateTimeField(default=datetime.datetime.now)
    context = peewee.TextField(default=None)

    class Meta:
        database = database

    def __str__(self):
        return self.name


# Create table, add some instances
#TestNameModel.create_table(True)

#res = TestNameModel.create(name="你好",age=10)

#print(res.id)



#res = TestNameModel.get(id=7)

#res.name = "444"

#res.save()

#print(res)

#删除

#TestNameModel.delete().where(TestNameModel.id == 7).execute()

#res = TestNameModel.select().where(TestNameModel.name=="你好")

#res = print_models(res)


# for item in res:

# 	print(item.id)
# 	item.cc = "123"

# 	print(item.cc)

# 修改



#TestNameModel.drop_table(True)


cursor = database.execute_sql('select * from user;')
# for row in cursor.fetchall():
#     print(row)

col_names = [col[0] for col in cursor.description] 
res = [dict(zip(col_names, row)) for row in cursor.fetchall()]

print(res)





database.close()


# # 导入pymysql
# import pymysql


# # md5加密
# import hashlib

# # 导入配置文件
# from settings import DB_NAME,DB_HOST,DB_USER,DB_PASSWORD

# # 数据密码加密方法
# def make_password(password):

# 	# 实例化md5对象
# 	md5 = hashlib.md5()

# 	# 转码
# 	sign_utf8 = str(password).encode(encoding="utf-8")

# 	# 加密操作
# 	md5.update(sign_utf8)

# 	# 返回密文
# 	return md5.hexdigest()



# class Db:

# 	# 初始化方法
# 	def __init__(self):

# 		# 链接数据库
# 		self.conn = pymysql.connect(host=DB_HOST,user=DB_USER,password=DB_PASSWORD,db=DB_NAME,charset="utf8",cursorclass=pymysql.cursors.DictCursor)


# 	# 删除方法
# 	def delete(self,sql):

# 		# 创建游标对象
# 		self.cursor = self.conn.cursor()
# 		# 执行sql语句
# 		self.cursor.execute(sql)
# 		# 提交sql
# 		self.conn.commit()

# 		return "delete ok"



# 	# 插入方法
# 	def insert(self,sql):

# 		# 创建游标对象
# 		self.cursor = self.conn.cursor()
# 		# 执行sql语句
# 		self.cursor.execute(sql)
# 		# 提交sql
# 		self.conn.commit()

# 		return "insert ok"

# 	# 修改方法
# 	def update(self,sql):

# 		# 创建游标对象
# 		self.cursor = self.conn.cursor()
# 		# 执行sql语句
# 		self.cursor.execute(sql)
# 		# 提交sql
# 		self.conn.commit()

# 		return "update ok"

# 	# 查询方法 单个数据
# 	def get_one(self,sql):

# 		# 创建游标对象
# 		self.cursor = self.conn.cursor()
# 		# 执行sql语句
# 		self.cursor.execute(sql)
# 		# 获取数据
# 		res = self.cursor.fetchone()
# 		# 返回结果集
# 		return res

# 	# 查询方法 多个数据
# 	def get_all(self,sql):

# 		# 创建游标对象
# 		self.cursor = self.conn.cursor()
# 		# 执行sql语句
# 		self.cursor.execute(sql)
# 		# 获取数据
# 		res = self.cursor.fetchall()
# 		# 返回结果集
# 		return res


# 	# 析构方法
# 	def __del__(self):

# 		# 关闭游标
# 		self.cursor.close()
# 		# 关闭数据库链接
# 		self.conn.close()


# mydb = Db()

# res = mydb.get_one(" select video from course where id = 6 ")

# print(res['video'])

